# coding: utf-8


class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None

    def set_data(self, data):
        self.data = data

    def get_data(self):
        return self.data

    def set_next(self, next):
        self.next = next

    def get_next(self):
        return self.next


class LinkedList(object):
    def __init__(self):
        self.head = None

    def is_empty(self):
        return self.head is None

    def add(self, data):
        temp = Node(data)
        temp.set_next(self.head)
        self.head = temp

    def remove(self, data):
        current = self.head
        previous = None
        found = False

        while current is not None and not found:
            if current.data == data:
                found = True
            else:
                previous = current
                current = current.get_next()

        if not found:
            return False

        if previous is None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())

    def size(self):
        count = 0
        current = self.head
        while current is not None:
            count += 1
            current = current.get_next()

        return count

    def search(self, data):
        current = self.head
        found = False

        while current is not None and not found:
            if current.get_data() == data:
                found = True
            else:
                current = current.get_next()

        return found

    def append(self, data):
        current = self.head

        if current is None:
            self.add(data)
        else:
            while current.get_next() is not None:
                current = current.get_next()

            current.set_next(Node(data))

    def insert(self, position, data):
        current = self.head
        found = False
        offset = 0
        previous = None

        while current is not None and not found:
            if offset == position:
                found = True
            else:
                previous = current
                current = current.get_next()
                offset += 1

        temp = Node(data)
        if previous is None:
            temp.set_next(self.head)
            self.head = temp
        else:
            temp.set_next(current)
            previous.set_next(temp)

    def index(self, data):
        current = self.head
        offset = 0

        while current is not None and current.get_data() != data:
            current = current.get_next()
            offset += 1

        return offset

    def pop(self, position):
        current = self.head
        found = False
        offset = 0
        previous = None

        while current is not None and not found:
            if offset == position:
                found = True
            else:
                previous = current
                current = current.get_next()
                offset += 1

        previous.set_next(current.get_next())


def print_linked(linked):
    current = linked.head
    out = ''
    while current is not None:
        out += str(current.get_data()) + ', '
        current = current.get_next()
    print(out)


linked = LinkedList()
for i in range(1, 20, 3):
    linked.add(i)


print_linked(linked)
print(linked.search(10), linked.search(15))

linked.remove(16)
print_linked(linked)

linked.append(100)
print_linked(linked)

linked.insert(1, 20)
print_linked(linked)

linked.insert(0, 18)
print_linked(linked)

print(linked.index(10))

linked.pop(4)
print_linked(linked)
